package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClaimBill;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.service.IClaimBillService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("claimBill")
public class ClaimBillController {
    @Autowired
    private IAccidentService service;
    @Autowired
    private IClaimBillService claimBillService;

    @RequestMapping("")
    @RequiresPermissions("claimBill:index")
    @PermissionName("理赔明细首页")
    private String index(){
        return "claimBill/list";
    }

    /*
     * 根据id查询单条 理赔明细
     * */
    @RequestMapping("get")
    @ResponseBody
    private ClaimBill get(Long id){
        return  claimBillService.selectByClientId(id);
    }

    /*
     * 提交验证,更改理赔表单状态,已验证,待填理赔明细状态
     * */
    @RequestMapping("updateInvalidateState")
    @ResponseBody
    private JsonResult updateInvalidateState(Long id){
        JsonResult result = new JsonResult();
        try {
            service.updateInvalidateState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("提交验证失败");
        }
        return result;
    }
    /*
     * 废弃数据
     * */
    @RequiresPermissions("claimBill:delete")
    @PermissionName("删除理赔明细数据")
    @RequestMapping("delete")
    @ResponseBody
    private JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("提交验证失败");
        }
        return result;
    }
    /*
     * 根据insuranceNo查询事故备案中对应的明细
     * */
    @RequestMapping("selectBillByInsuranceNo")
    @ResponseBody
    private List<ClaimBill> selectBillByInsuranceNo(String insuranceNo){
        return claimBillService.selectBillByInsuranceNo(insuranceNo);
    }
    /*
     * 拒审后删除事故备案中对应的明细
     * */
    @RequestMapping("deleteByInsuranceNo")
    @ResponseBody
    private JsonResult deleteByInsuranceNo(String insuranceNo){
        JsonResult result = new JsonResult();
        try {
            claimBillService.deleteByInsuranceNo(insuranceNo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("删除数据失败");
        }
        return result;
    }

    /*
     * 查询状态state=2,state=3的理赔明细
     * */
    @RequestMapping("accidentAndClaimBill")
    @ResponseBody
    private PageResult accidentAndClaimBill(QueryObject qo){
        return  service.queryValidateUnBill(qo);
    }

    /*
     * 保存理赔明细
     * */
    @RequestMapping("save")
    @ResponseBody
    private JsonResult save(ClaimBill entity,Long accident_Id){
        JsonResult result = new JsonResult();
        try {
            claimBillService.save(entity);
            service.updateBilledAudit(accident_Id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存理赔明细失败");
        }
        return result;
    }
    /*
     * 更新理赔明细
     * */
    @RequestMapping("update")
    @ResponseBody
    private JsonResult update(ClaimBill entity){
        JsonResult result = new JsonResult();
        try {
            claimBillService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("保存理赔明细失败");
        }
        return result;
    }
    /*
     * 更新状态为:待审核
     * */
    @RequestMapping("changeAuditState")
    @ResponseBody
    private JsonResult changeAuditState(Long id){
        JsonResult result = new JsonResult();
        try {
            claimBillService.changeAuditState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("提审数据失败");
        }
        return result;
    }
    /*
     * 更新状态为:审核驳回
     * */
    @RequestMapping("changeSateByRefuse")
    @ResponseBody
    private JsonResult changeSateByRefuse(Long id){
        JsonResult result = new JsonResult();
        try {
            claimBillService.changeSateByRefuse(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("拒审数据失败");
        }
        return result;
    }
}
